package org.musicsweet.service;

import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.musicsweet.constant.StringConstant;
import org.musicsweet.entity.MusicSong;
import org.musicsweet.entity.SysUser;
import org.musicsweet.mapper.MusicSongMapper;
import org.musicsweet.result.ResultData;
import org.musicsweet.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

/**
 * 歌曲Service
 */
@Slf4j
@Service
public class MusicSongService {

    @Autowired
    private MusicSongMapper musicSongMapper;

    @Autowired
    private SysUserService sysUserService;


    /**
     * 根据id查询歌曲
     *
     * @param musicSong
     * @return
     */
    public ResultData<MusicSong> getMusicSongById(MusicSong musicSong) {
        log.info("根据id查询歌曲:"+ JSON.toJSONString(musicSong));
        if(StringUtils.isEmpty(musicSong.getId())){
            return ResultData.createFail("id不能为空");
        }
        musicSong.setDel(StringConstant.DEL_NO);
        MusicSong data = musicSongMapper.getMusicSongById(musicSong);
        return ResultData.successData("查询成功",data);
    }

    /**
     * 多条件查询歌曲
     *
     * @param musicSong
     * @return
     */
    public ResultData<List<MusicSong>> getMusicSongList(MusicSong musicSong) {
        log.info("多条件查询歌曲:"+ JSON.toJSONString(musicSong));
        musicSong.setDel(StringConstant.DEL_NO);
        List<MusicSong> data = musicSongMapper.getMusicSongList(musicSong);
        return ResultData.successData("查询成功",data);
    }


    /**
     * 添加歌曲
     * @param musicSong
     * @param token
     * @return
     */
    @Transactional
    public ResultData addMusicSong(MusicSong musicSong,String token) {
        musicSong.setId(StringUtil.getUUID());
        musicSong.setCreateTime(new Date());
        SysUser currentUser = sysUserService.getSysUserByToken(token).getData();
        musicSong.setCreateUserId(currentUser.getId());
        musicSong.setDel(StringConstant.DEL_NO);
        log.info("添加歌曲:"+ JSON.toJSONString(musicSong));
        int i = musicSongMapper.addMusicSong(musicSong);
        if(i>0){
            return ResultData.createSuccess(null);
        }else{
            return ResultData.createFail("添加歌曲失败");
        }
    }


    /**
     * 修改歌曲
     *
     * @param musicSong
     * @return
     */
    @Transactional
    public ResultData updateMusicSong(MusicSong musicSong,String token) {
        if(StringUtils.isEmpty(musicSong.getId())){
            return ResultData.createFail("id不能为空");
        }
        SysUser currentUser = sysUserService.getSysUserByToken(token).getData();
        musicSong.setUpdateUserId(currentUser.getId());
        musicSong.setUpdateTime(new Date());
        log.info("修改歌曲:"+ JSON.toJSONString(musicSong));
        int i = musicSongMapper.updateMusicSong(musicSong);
        if(i>0){
            return ResultData.createSuccess(null);
        }else{
            return ResultData.createFail("修改歌曲失败");
        }
    }


    /**
     * 删除歌曲
     *
     * @param musicSong
     * @return
     */
    @Transactional
    public ResultData delMusicSong(MusicSong musicSong) {
        if(StringUtils.isEmpty(musicSong.getId())){
            return ResultData.createFail("id不能为空");
        }
        musicSong.setDel(StringConstant.DEL_OK);
        log.info("删除歌曲:"+ JSON.toJSONString(musicSong));
        int i = musicSongMapper.deleteMusicSong(musicSong);
        if(i>0){
            return ResultData.createSuccess(null);
        }else{
            return ResultData.createFail("删除歌曲失败");
        }
    }
}
